<?php
class Refund extends  CS_Controller
{
    public function _init()
    {
        $this->load->model('tourism_order_product_model', 'tourism_order_product');
        $this->load->model('tourism_order_refund_model', 'tourism_order_refund');
    }

    public function apply($orderProductId)
    {
        $result = $this->tourism_order_product->findById($orderProductId);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('未找到订单商品信息');
        }
        $this->data['orderProductInfo'] =  $result->row();
        $this->load->view('tourism/refund/refund', $this->data);
    }

    public function details($id)
    {
        $result = $this->tourism_order_refund->findByOrderProductId($id);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('该订单没有退款信息');
        }
        $this->data['refund'] = $result->row();
        $this->load->view('tourism/refund/details', $this->data);
    }

    public function confirmPost()
    {
        $is_refund = $this->tourism_order_refund->selectByOrderProductId($this->input->post('order_product_id'));
        if(!empty($is_refund)){
            $this->alertJumpPre('产品已经退款！不需要再次退款！');
        }
        $postData = $this->input->post();
        $imageData = '';
        if (!empty($_FILES['images']['name'])) {
            $imageData = $this->dealWithImages('images', '', 'tourism');
            if ($imageData == false) {
                $this->alertJumpPre('图片上传失败');
            }
            $ifResize = $this->dealWithImagesResize($imageData, '60', '60');
            if ($ifResize == false) {
                $this->alertJumpPre('缩略图上传失败');
            }
        }
        $result = $this->tourism_order_product->findById($postData['order_product_id']);
        if($result->num_rows() <= 0){
            $this->alertJumpPre('未找到订单商品信息');
        }
        $orderProductInfo =  $result->row();
        $postData['order_id'] = $orderProductInfo->order_id;
        $postData['order_product_id'] = $orderProductInfo->order_product_id;
        $postData['goods_attr_id'] = $orderProductInfo->goods_attr_id;
        $postData['existing'] = $orderProductInfo->refund_num;
        $postData['seller_uid'] = $orderProductInfo->seller_uid;
        $postData['uid'] = $this->uid;
        $refund_num = bcsub($orderProductInfo->refund_num, $postData['number']);
        $this->db->trans_begin();
        $updateNum = $this->tourism_order_product->updateProduct(array('order_product_id'=>$orderProductInfo->order_product_id,'refund_num'=>$refund_num));
        $refund = $this->tourism_order_refund->insert($postData, $imageData);
        if (!$updateNum || !$refund || $this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $this->alertJumpPre('退款申请失败');
        } else {
            $this->db->trans_commit();
            $this->success('tourism/refund/refundList', '', '退款申请成功');
        }
    }

    public function refundList()
    {
        $this->data['result'] = $this->tourism_order_refund->findRefundList($this->uid);
        $this->load->view('tourism/refund/list',$this->data);
    }
}